package cn.iocoder.yudao.module.system.controller.admin.userRole;

import cn.iocoder.yudao.module.system.dal.dataobject.permission.UserRoleDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.userRole.vo.*;
import cn.iocoder.yudao.module.system.convert.userRole.UserRoleConvert;
import cn.iocoder.yudao.module.system.service.userRole.UserRoleService;

@Api(tags = "管理后台 - 用户和角色关联")
@RestController
@RequestMapping("/system/user-role")
@Validated
public class UserRoleController {

    @Resource
    private UserRoleService userRoleService;

    @PostMapping("/create")
    @ApiOperation("创建用户和角色关联")
    @PreAuthorize("@ss.hasPermission('system:user-role:create')")
    public CommonResult<Long> createUserRole(@Valid @RequestBody UserRoleCreateReqVO createReqVO) {
        return success(userRoleService.createUserRole(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新用户和角色关联")
    @PreAuthorize("@ss.hasPermission('system:user-role:update')")
    public CommonResult<Boolean> updateUserRole(@Valid @RequestBody UserRoleUpdateReqVO updateReqVO) {
        userRoleService.updateUserRole(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除用户和角色关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:user-role:delete')")
    public CommonResult<Boolean> deleteUserRole(@RequestParam("id") Long id) {
        userRoleService.deleteUserRole(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得用户和角色关联")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:user-role:query')")
    public CommonResult<UserRoleRespVO> getUserRole(@RequestParam("id") Long id) {
        UserRoleDO userRole = userRoleService.getUserRole(id);
        return success(UserRoleConvert.INSTANCE.convert(userRole));
    }


    @GetMapping("/list")
    @ApiOperation("获得用户和角色关联列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:user-role:query')")
    public CommonResult<List<UserRoleRespVO>> getUserRoleList(@RequestParam("ids") Collection<Long> ids) {
        List<UserRoleDO> list = userRoleService.getUserRoleList(ids);
        return success(UserRoleConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得用户和角色关联分页")
    @PreAuthorize("@ss.hasPermission('system:user-role:query')")
    public CommonResult<PageResult<UserRoleRespVO>> getUserRolePage(@Valid UserRolePageReqVO pageVO) {
        PageResult<UserRoleDO> pageResult = userRoleService.getUserRolePage(pageVO);
        return success(UserRoleConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出用户和角色关联 Excel")
    @PreAuthorize("@ss.hasPermission('system:user-role:export')")
    @OperateLog(type = EXPORT)
    public void exportUserRoleExcel(@Valid UserRoleExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserRoleDO> list = userRoleService.getUserRoleList(exportReqVO);
        // 导出 Excel
        List<UserRoleExcelVO> datas = UserRoleConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户和角色关联.xls", "数据", UserRoleExcelVO.class, datas);
    }

}
